package com.example.myapplication.management;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.Penson.ModifyPasswordActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ActivityListBean;
import com.example.myapplication.ShuJu.EventDetailsBean;
import com.example.myapplication.ShuJu.ReviewBean;
import com.example.myapplication.ShuJu.ReviewNumBean;
import com.example.myapplication.apter.EventDetailsAdapter;
import com.example.myapplication.apter.NewsAdapter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class EventDetailsActivity extends AppCompatActivity {

    private String rowsBeanID;
    private Button btn_back;
    private TextView tv_activity_name;
    private ImageView iv_activity_image;
    private TextView tv_activity_details;
    private TextView reviewnum;
    private RecyclerView re_comments;
    private Button btn_comment;
    private Button btn_join_activity;
    private Button btn_viewcomment;
    private EditText getviewcommit;
    private OkHttpClient okHttpClient;
    private Gson gson;
    private Request  request;
    private EventDetailsBean  eventDetailsBean;
    private ReviewBean  reviewBean;
    private Handler handler;
    private ReviewNumBean reviewNumBean;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_details);
        Intent intent = getIntent();
        rowsBeanID = intent.getStringExtra("RowsBeanID");
        initview();
        sendbean();
    }

    private void sendbean() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/activity/activity/"+rowsBeanID)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                eventDetailsBean = gson.fromJson(string, EventDetailsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        tv_activity_name.setText(eventDetailsBean.getData().getCategoryName());
                        Glide.with(EventDetailsActivity.this).load("http://124.93.196.45:10001"+eventDetailsBean.getData().getImgUrl()).into(iv_activity_image);
                        tv_activity_details.setText(eventDetailsBean.getData().getName());
                        Log.d("eventDetailsBean",eventDetailsBean.getData().getId()+"");
                        //根据id获得不同的评论
                        bingreview(eventDetailsBean.getData().getId());
                        //根据id获得评论条数
                        getreview(eventDetailsBean.getData().getId());
                        //根据id去发布评论
                        Sendcomment(eventDetailsBean.getData().getId());
                        //根据id去报名该活动
                        Registration(eventDetailsBean.getData().getId());
                    }
                });
            }
        });
    }

    private void Registration(Integer id) {
        btn_join_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("activityId",String.valueOf(id));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(jsonObject.toString(),JSON);
                request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/api/activity/signup")
                        .addHeader("Authorization", TokenBean.token)
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(EventDetailsActivity.this.getApplicationContext(),"报名成功",Toast.LENGTH_SHORT);
                            }
                        });
                    }
                });
            }
        });
            }


    private void Sendcomment(Integer id) {
        btn_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String CommentContent = getviewcommit.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("activityId",String.valueOf(id));
                    jsonObject.put("content",CommentContent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(jsonObject.toString(),JSON);
                request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/api/activity/comment")
                        .addHeader("Authorization",TokenBean.token)
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        Log.d("Sendcomment",string);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //重新获取评论信息
                                bingreview(eventDetailsBean.getData().getId());
                                Toast.makeText(EventDetailsActivity.this.getApplicationContext(),"发表评论成功",Toast.LENGTH_SHORT);
                            }
                        });
                    }
                });
            }
        });
    }

    private void getreview(Integer id) {
        btn_viewcomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/api/activity/comment/number?activityId="+id)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        reviewNumBean = gson.fromJson(string, ReviewNumBean.class);
                        gson.fromJson(string, ReviewBean.class);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                reviewnum.setVisibility(View.VISIBLE);
                                reviewnum.setText("当前有"+String.valueOf(reviewNumBean.getCommentNum())+"评论");
                            }
                        });
                    }
                });
            }
        });
    }

    private void bingreview(Integer ID) {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/activity/comment/list?activityId=+"+ID+"&pageNum=1&pageSize=8")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                reviewBean = gson.fromJson(string, ReviewBean.class);
                gson.fromJson(string, ReviewBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bingeventDetailsBean();
                    }
                });
            }
        });
    }

    private void bingeventDetailsBean() {
        EventDetailsAdapter eventDetailsAdapter = new EventDetailsAdapter(EventDetailsActivity.this,reviewBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(EventDetailsActivity.this);
        re_comments.setLayoutManager(linearLayoutManager);
        re_comments.setAdapter(eventDetailsAdapter);
    }

    private void initview() {
        btn_back = findViewById(R.id.btn_back);
        tv_activity_name = findViewById(R.id.tv_activity_name);
        iv_activity_image = findViewById(R.id.iv_activity_image);
        tv_activity_details = findViewById(R.id.tv_activity_details);

        re_comments = findViewById(R.id.re_comments);
        btn_comment = findViewById(R.id.btn_comment);
        btn_join_activity = findViewById(R.id.btn_join_activity);
        btn_viewcomment = findViewById(R.id.btn_viewcomment);
        reviewnum = findViewById(R.id.reviewnum);
        getviewcommit = findViewById(R.id.getviewcommit);

        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }
}